package com.zhijian.medical.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhijian.medical.dao.local.RoutinePatientDiagnoseCollectMapper;
import com.zhijian.medical.dao.local.RoutinePatientDiagnoseDiseaseMapper;
import com.zhijian.medical.dao.local.RoutinePatientDiagnoseMapper;
import com.zhijian.medical.entity.po.RoutinePatientDiagnoseCollect;
import com.zhijian.medical.entity.po.RoutinePatientDiagnoseDisease;
import com.zhijian.medical.entity.vo.response.RoutinePatientDiagnoseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 疾病业务处理
 */
@Service
public class RoutinePatientDiagnoseDiseaseService {

    @Autowired
    RoutinePatientDiagnoseDiseaseMapper routinePatientDiagnoseDiseaseMapper;

    @Autowired
    RoutinePatientDiagnoseMapper routinePatientDiagnoseMapper;

    @Autowired
    RoutinePatientDiagnoseCollectMapper routinePatientDiagnoseCollectMapper;

    public RoutinePatientDiagnoseDisease addDisease(RoutinePatientDiagnoseDisease routinePatientDiagnoseDisease){
        Date now = new Date();
        routinePatientDiagnoseDisease.setCreateTime(now);
        routinePatientDiagnoseDisease.setUpdateTime(now);
        routinePatientDiagnoseDiseaseMapper.insert(routinePatientDiagnoseDisease);
        return routinePatientDiagnoseDisease;
    }

    public List<RoutinePatientDiagnoseDisease> readDiseaseList(RoutinePatientDiagnoseDisease routinePatientDiagnoseDisease, Integer type){
        //查询当前人收藏的疾病
        QueryWrapper<RoutinePatientDiagnoseDisease> rootQuery = new QueryWrapper<>();
        rootQuery.eq("user_id", routinePatientDiagnoseDisease.getUserId());
        List<RoutinePatientDiagnoseDisease> dataList = routinePatientDiagnoseDiseaseMapper.selectList(rootQuery);
        if (type != null && type == 0){
            return dataList;
        }
        List<Long> diseaseIdList = dataList.stream().map(RoutinePatientDiagnoseDisease::getId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(diseaseIdList)){
            //查询收藏的疾病报告列表
            QueryWrapper<RoutinePatientDiagnoseCollect> nodeQuery = new QueryWrapper<>();
            nodeQuery.in("disease_id", diseaseIdList);
            List<RoutinePatientDiagnoseCollect> routinePatientDiagnoseCollectList =
                    routinePatientDiagnoseCollectMapper.selectList(nodeQuery);
            List<Long> diagnoseIdList = routinePatientDiagnoseCollectList.stream().map(RoutinePatientDiagnoseCollect::getTypeId).collect(Collectors.toList());
            List<RoutinePatientDiagnoseVo> routinePatientDiagnoseVos = routinePatientDiagnoseMapper.selectOneByIdList(diagnoseIdList);
            Map<Long, RoutinePatientDiagnoseVo> map = routinePatientDiagnoseVos.stream().collect(Collectors.toMap(RoutinePatientDiagnoseVo::getId, e -> e));
            //给收藏添加病理号
            for (RoutinePatientDiagnoseCollect routinePatientDiagnoseCollect : routinePatientDiagnoseCollectList){
                RoutinePatientDiagnoseVo d  = map.get(routinePatientDiagnoseCollect.getTypeId());
                if (d != null){
                    routinePatientDiagnoseCollect.setConventionPathologyNumber(d.getConventionPathologyNumber());
                    routinePatientDiagnoseCollect.setCollectName(d.getPatientName());
                }
            }
            Map<Long, List<RoutinePatientDiagnoseCollect>> nodeMap = routinePatientDiagnoseCollectList.stream().collect(Collectors.groupingBy(RoutinePatientDiagnoseCollect::getDiseaseId));
            for (RoutinePatientDiagnoseDisease rpdd : dataList ){
                List<RoutinePatientDiagnoseCollect> children = nodeMap.get(rpdd.getId());
//                if (CollectionUtil.isNotEmpty(children)){
//                    for (RoutinePatientDiagnoseCollect routinePatientDiagnoseCollect : children){
//                        routinePatientDiagnoseCollect.setCollectName(rpdd.getName());
//                    }
//                }
                rpdd.setChildren(children);
            }
        }
        return dataList;
    }
}
